package strcy.cn.market.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import strcy.cn.market.pojo.OrderDetails;
import strcy.cn.market.pojo.Orders;
import strcy.cn.market.pojo.VO.OrderRequest;
import strcy.cn.market.service.OrdersService;
import strcy.cn.market.utils.PageBean;
import strcy.cn.market.utils.Result;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    //创建订单 商品列表id ，价格，数量，创建订单详情表
    @PostMapping("/add")
    public Result addOrder(@RequestBody OrderRequest orderRequest) {
        System.out.println(orderRequest);
        Orders orders = orderRequest.getOrders();
        List<OrderDetails> orderDetailsList = orderRequest.getOrderDetails();
        //生成订单ID
        String orderId = UUID.randomUUID().toString();
        orders.setOrderId(orderId);
        //新建订单
        ordersService.add(orders);

        // 插入订单详情
        for (OrderDetails item : orderDetailsList) {
            item.setOrderId(orderId); // 将订单ID设置到订单详情中
            ordersService.insertOrderDetail(item);
        }
        return Result.success();
    }
    //修改订单状态
    @PutMapping("/update")
    public Result updateOrder( String orderId,Integer orderStatus) {
        ordersService.updatePayTime(orderId,orderStatus);
        return Result.success();
    }
    //获取个人订单列表
    @GetMapping("/listByUid")
    public Result<List<Orders>> getListByUid() {
        List<Orders> list = ordersService.list();
        return Result.success(list);
    }
    //获取订单列表
    @GetMapping("/list")
    public Result<PageBean<Orders>> getList(Integer pageNum,Integer pageSize) {
        PageBean<Orders> list = ordersService.lists(pageNum,pageSize);
        return Result.success(list);
    }

}
